package cn.tedu.cwgl.service.impl;

import cn.tedu.cwgl.entity.House;
import cn.tedu.cwgl.entity.Item;
import cn.tedu.cwgl.mapper.MoneyManageMapper;
import cn.tedu.cwgl.service.MoneyManageService;
import cn.tedu.cwgl.util.PageModel;
import cn.tedu.cwgl.util.Result;
import cn.tedu.cwgl.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class MoneyManageServiceImpl implements MoneyManageService {

    @Autowired
    MoneyManageMapper moneyManageMapper;

    @Override
    public Result<Item> findMoneyManage(PageModel model) {
        List<Item> itemList = moneyManageMapper.getMoneyMange(model);
        if (itemList.size() >= 0) {
            Result result = ResultUtil.success(itemList);
            result.setTotal(moneyManageMapper.getTotal(model));
            if (result.getTotal() == 0) {
                result.setMsg("刷新重试");
            } else {
                result.setMsg("数据获取成功");
            }
            return result;
        } else {
            return ResultUtil.unSuccess("数据获取失败");
        }
    }

    @Override
    public int updateMon(Item item) {

        return moneyManageMapper.updateMon(item);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int delect(int id, int houseid) throws Exception {
        int i = moneyManageMapper.delectBefore(houseid);
        int delect = moneyManageMapper.delect(id);
        if (i + delect != 2) {
            throw new Exception("删除不同步");
        }
        return delect;
    }
//    @Override
//    public int delectAfter(int id) {
//        return moneyManageMapper.delectBefore(id);
//    }

    @Override
    public int addMonBefore(House house) {
        moneyManageMapper.addMonBefore(house);

        return house.getId();

    }

    @Override
    public int addMon(Item item) {
        return moneyManageMapper.addMon(item);
    }


}
